import cv2
import numpy as np

img = cv2.imread("../images/morph.png")
img_gary = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

img_canny = cv2.Canny(img_gary, 30, 70)

lines = cv2.HoughLinesP(
    img_canny,
    rho=1,
    theta=np.pi/180,
    threshold=30,       # 累加器阈值     霍夫空间中交线的统计次数
    minLineLength=0,    # 检测到的直线的最小长度，小于该长度的直线会被忽略
    maxLineGap=100      # 同一行点之间的最大允许间隔，小于该间隔的点会被认为在同一条直线上
                        # 车道线是一条直线，但是会有间隔
)
print(lines)
print(lines.shape)

hough_img = np.zeros_like(img, dtype="u1")
for line in lines:
    print(line)
    x1, y1, x2, y2 = line[0]
    cv2.line(hough_img, (x1,y1), (x2, y2), (0,0,255))
cv2.imshow("img_canny", img_canny)
cv2.imshow("hough_img", hough_img)
cv2.waitKey(0)
